package com.log4jviewer.ui.preferences.filters;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;

import com.log4jviewer.filters.FilterItemModel;

/**
 * Create possibility enable/disable the rule in Rules Table
 * 
 * @author <a href="mailto:solid.danil@gmail.com">Daniil Lopatin</a>
 */
public class RulesEnableCellEditor extends EditingSupport {

    private final TableViewer tableViewer;

    public RulesEnableCellEditor(final TableViewer tableViewer) {
        super(tableViewer);
        this.tableViewer = tableViewer;
    }

    @Override
    protected CellEditor getCellEditor(final Object element) {
        return new CheckboxCellEditor(tableViewer.getControl().getParent(), SWT.CHECK);
    }

    @Override
    protected boolean canEdit(final Object element) {
        return true;
    }

    @Override
    protected Object getValue(final Object element) {
        return ((FilterItemModel) element).isEnabled();
    }

    @Override
    protected void setValue(final Object element, final Object value) {
        FilterItemModel filterItemModel = (FilterItemModel) element;
        filterItemModel.setEnabled((Boolean) value);
        tableViewer.update(element, null);
    }
}
